package com.ken.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ken.context.BaseContext;
import com.ken.dto.SetmealDTO;
import com.ken.dto.SetmealPageQueryDTO;
import com.ken.entity.Setmeal;
import com.ken.result.PageResult;
import com.ken.result.Result;
import com.ken.service.MealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api("套餐相关接口")
public class MealController {
    @Autowired
    private MealService mealService;

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询套餐")
    public Result<Setmeal>  findById(@PathVariable String id){
        Setmeal byId = mealService.getById(id);
        return Result.success(byId);
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("菜品分页查询参数：{}",setmealPageQueryDTO);
        Page<Setmeal> page=mealService
                .query()
                .like(setmealPageQueryDTO.getCategoryId()!=null,"category_id",setmealPageQueryDTO.getCategoryId())
                .like(StrUtil.isNotBlank(setmealPageQueryDTO.getName()),"name",setmealPageQueryDTO.getName())
                .like(setmealPageQueryDTO.getStatus()!=null,"status",setmealPageQueryDTO.getStatus())
                .page(new Page<>(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize()));
        return Result.success(new PageResult(page.getTotal(),page.getRecords()));
    }

    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmeal.getCategoryId()")
    public Result<String> insertMeal(@RequestBody SetmealDTO setmeal){
        log.info("新增套餐参数：{}",setmeal);
        mealService.insetMealAndDish(setmeal);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result<String> updateMeal(@RequestBody SetmealDTO setmealDTO){
        mealService.updateSetMealAndMealDish(setmealDTO);
        return Result.success();
    }

    @ApiOperation("批量删除")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result<String> deleteMeal(@RequestParam List<Long> ids){
        mealService.deleteById(ids);
        return Result.success();
    }

    @ApiOperation("套餐起售停售")
    @PostMapping("/status/{status}")
    public Result<String> setStatusById(@PathVariable String status,@RequestParam String id){
        Setmeal byId = mealService.getById(id);
        byId.setStatus(Integer.valueOf(status));
        byId.setUpdateTime(LocalDateTime.now());
        Long currentId = BaseContext.getCurrentId();
        byId.setUpdateUser(currentId);
        mealService.update(byId,new LambdaQueryWrapper<Setmeal>().eq(Setmeal::getId,id));
        return Result.success();
    }
}
